﻿
namespace AdventureWorksService.Web
{
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.ComponentModel.DataAnnotations;
	using System.Data;
	using System.Linq;
	using System.ServiceModel.DomainServices.Hosting;
	using System.ServiceModel.DomainServices.Server;


	[EnableClientAccess()]
	public class AdventureWorksDomainService : DomainService// LinqToEntitiesDomainService<conAdventureWorks>
	{
		public AdventureWorksDomainService()
		{
		}

		private void LineDelay()
		{
#if BUSY
			System.Threading.Thread.Sleep(1000);
#endif
			string sdelay = (string)System.Web.HttpContext.Current.Session["servicedelay"];
			if(!string.IsNullOrEmpty(sdelay)) {
				int delay;
				int.TryParse(sdelay, out delay);
				if(delay > 0) System.Threading.Thread.Sleep(delay);
			}
		}

		protected override bool ExecuteChangeSet()
		{
			return base.ExecuteChangeSet();
		}
		public override System.Collections.IEnumerable Query(QueryDescription queryDescription, out IEnumerable<ValidationResult> validationErrors, out int totalCount)
		{
			LineDelay();
			return base.Query(queryDescription, out validationErrors, out totalCount);
		}
		public override bool Submit(ChangeSet changeSet)
		{
			LineDelay();
			return base.Submit(changeSet);
		}

		//-- Product ------------------------------------------------------
		public IQueryable<Product> GetProduct()
		{
			ProductList li = System.Web.HttpContext.Current.Session["products"] as ProductList;
			var query = from n in li select n;
			return query.AsQueryable();
		}
		public void DeleteProduct(Product item_)
		{
			ProductList li = System.Web.HttpContext.Current.Session["products"] as ProductList;
			var item = li.FirstOrDefault(p => p.ProductID == item_.ProductID);
			if(item != null) {
				li.Remove(item);
			}
		}

		public void InsertProduct(Product item_)
		{
			ProductList li = System.Web.HttpContext.Current.Session["products"] as ProductList;
			li.Add(item_);
		}

		public void UpdateProduct(Product item_)
		{
			ProductList li = System.Web.HttpContext.Current.Session["products"] as ProductList;
			var item = li.FirstOrDefault(p => p.ProductID == item_.ProductID);
			if(item != null) {
				item.Name = item_.Name;
				item.ProductNumber = item_.ProductNumber;
				item.Color = item_.Color;
				item.StandardCost = item_.StandardCost;
				item.ListPrice = item_.ListPrice;
				item.Size = item_.Size;
				item.Weight = item_.Weight;
				item.SellStartDate = item_.SellStartDate;
			}
		}

		//-- vwProduct ------------------------------------------------------
		public IQueryable<vwProduct> GetVwProduct()
		{
			ProductList li = System.Web.HttpContext.Current.Session["products"] as ProductList;
			var query = from n in li select new vwProduct { 
				ProductID = n.ProductID, Name = n.Name, Color = n.Color, Size = n.Size };
			return query.AsQueryable();
		}
		public void DeleteProduct(vwProduct item_)
		{
			ProductList li = System.Web.HttpContext.Current.Session["products"] as ProductList;
			var item = li.FirstOrDefault(p => p.ProductID == item_.ProductID);
			if(item != null) {
				li.Remove(item);
			}
		}
		public void InsertVwProduct(vwProduct item_)
		{
			// fiktivní operace, aby bylo možné vložit řádek do view (DetailVloži DataForm)
		}
		public void UpdateVwProduct(vwProduct item_)
		{
			// fiktivní operace, aby byl možný update řádek do view (DetailVloži DataForm)
		}

		//-- Customer View --------------------------
		public IQueryable<vwCustomer> GetVwCustomer()
		{
			CustomerList li = System.Web.HttpContext.Current.Session["customers"] as CustomerList;
			var query = from n in li select new vwCustomer { CustomerID = n.CustomerID, CompanyName = n.CompanyName, FirstName = n.FirstName, LastName = n.LastName };
			return query.AsQueryable();
		}
		public void DeleteCustomer(vwCustomer customer)
		{
			CustomerList li = System.Web.HttpContext.Current.Session["customers"] as CustomerList;
			var cu = li.FirstOrDefault(p => p.CustomerID == customer.CustomerID);
			if(cu != null) {
				li.Remove(cu);
			}
		}
		public void InsertVwCustomer(vwCustomer customer)
		{
			// fiktivní operace, aby bylo možné vložit řádek do view (DetailVloži DataForm)
		}
		public void UpdateVwCustomer(vwCustomer customer)
		{
			// fiktivní operace, aby byl možný update řádek do view (DetailVloži DataForm)
		}
		//-- customer -----------------------------
		public IQueryable<Customer> GetCustomer()
		{
			CustomerList li = System.Web.HttpContext.Current.Session["customers"] as CustomerList;
			var query = from n in li select n;
			return query.AsQueryable();
		}

		public void DeleteCustomer(Customer customer)
		{
			CustomerList li = System.Web.HttpContext.Current.Session["customers"] as CustomerList;
			var cu = li.FirstOrDefault(p => p.CustomerID == customer.CustomerID);
			if(cu != null) {
				li.Remove(cu);
			}
		}

		public void InsertCustomer(Customer customer)
		{
			CustomerList li = System.Web.HttpContext.Current.Session["customers"] as CustomerList;
			li.Add(customer);
		}

		public void UpdateCustomer(Customer customer)
		{
			CustomerList li = System.Web.HttpContext.Current.Session["customers"] as CustomerList;
			var cu = li.FirstOrDefault(p => p.CustomerID == customer.CustomerID);
			if(cu != null) {
				cu.CompanyName = customer.CompanyName;
				cu.FirstName = customer.FirstName;
				cu.LastName = customer.LastName;
				cu.EmailAddress = customer.EmailAddress;
				cu.Phone = customer.Phone;
			}
		}

	}
}


